//
//  AddAddressViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/23.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "BRAddressPickerView.h"

#import "AddAddressViewController.h"

@interface AddAddressViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *nameTF;

@property (weak, nonatomic) IBOutlet UITextField *phoneTF;

@property (weak, nonatomic) IBOutlet UILabel *addressControl;

@property (weak, nonatomic) IBOutlet UITextField *addressTF;

@property (weak, nonatomic) IBOutlet UISwitch *addressDefSwitch;

@property (weak, nonatomic) IBOutlet UIButton *saveBtn;

@property (nonatomic, assign) BOOL isDef;

@end

@implementation AddAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
}

- (void)initView {
    
    self.title = @"添加收货地址";

    self.nameTF.tag = 100;
    self.phoneTF.tag = 200;
    self.addressTF.tag = 300;

    self.nameTF.delegate = self;
    self.phoneTF.delegate = self;
    self.addressTF.delegate = self;
    
    if (self.model.udId) {
        self.nameTF.text = self.model.userName;
        self.phoneTF.text = self.model.phone;
        self.addressTF.text = self.model.address;
        self.addressControl.text = [NSString stringWithFormat:@"%@-%@-%@",self.model.province,self.model.city,self.model.area];
        
        if (self.model.isDefault == 1) {
            self.addressDefSwitch.on = YES;
            self.isDef = YES;
        } else {
            self.addressDefSwitch.on = NO;
            self.isDef = NO;

        }
    }

    WEAKSELF
    self.addressControl.userInteractionEnabled = YES;
    [self.addressControl addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {
       
        [weakSelf showAddressPicker];
    }];
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    switch (textField.tag) {
        case 100:{
            self.model.userName = textField.text;
        } break;
        case 200:{
            self.model.phone = textField.text;
        } break;
        case 300:{
            self.model.address = textField.text;
        } break;
        default:{

        } break;
    }
}

- (IBAction)switchAction:(id)sender {
    self.isDef = !self.isDef;
    self.addressDefSwitch.on = self.isDef;
    
    if (self.isDef) {
        self.model.isDefault = 1;
    } else {
        self.model.isDefault = 2;
    }
}


- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    [self.saveBtn pp_clipCorners:UIRectCornerAllCorners radius:self.saveBtn.frame.size.height/2];
}

- (IBAction)handleAction:(id)sender {
    
    [self.view endEditing:YES];
    if (self.model.udId) {
        [self request_updateUserAddress];
    } else {
        [self request_saveAddress];
    }
}


-(void)showAddressPicker{
    
    [self.view endEditing:YES];
    
    WEAKSELF
    [BRAddressPickerView showAddressPickerWithMode:BRAddressPickerModeArea selectIndexs:@[@(11),@(0),@(3)] isAutoSelect:NO resultBlock:^(BRProvinceModel * _Nullable province, BRCityModel * _Nullable city, BRAreaModel * _Nullable area) {

        weakSelf.model.province = province.name;
        weakSelf.model.city = city.name;
        weakSelf.model.area = area.name;

        weakSelf.addressControl.text = [NSString stringWithFormat:@"%@%@%@",province.name,city.name,area.name];
    }];
}

- (AddressItemModel *)model {
    if (!_model) {
        _model = [[AddressItemModel alloc] init];
    }
    return _model;
}

- (void)request_saveAddress {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    dic = [self.model modelToJSONObject];
    [dic setValue:[TTUserModel sharedObject].userId forKey:@"userId"];
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_AddAddress];
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {

        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
    }];

}

- (void)request_updateUserAddress {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    dic = [self.model modelToJSONObject];
    [dic setValue:[TTUserModel sharedObject].userId forKey:@"userId"];
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_updateAddress];
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {

        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
    }];

}

@end
